<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-01-13 11:05:55
 * @Last Modified time: 2023-02-23 15:47:35
 */
namespace app\sys\controller\user;

use app\sys\controller\Base;
use think\facade\{
    App
};
use app\common\services\user\UserService;
use app\common\services\store\OrderService;
/**
 * 用户
 * Class User 
 * @package app\sys\controller\user
 */
class User extends Base
{
    /**
     * User constructor.
     * @param App $app
     */
    public function __construct(App $app,UserService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 用户列表
     * @method(GET)
     */
    public function list()
    {
        $param = $this->request->getMore([]);
        $this->success($this->service->getUserList($param));
    }

    /**
     * 设置用户分组
     * @method(PUT)
     */
    public function updateGroup(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'group_id',
        ]);
        if (!$data['group_id']) $this->error('分组ID不能为空！');
        if($this->service->update($id, $data)){
            $this->success('设置分组成功!');
        }
        $this->error('设置分组失败!');
    }

    /**
     * 用户详细 
     * @noAuth(true)
     * @method(GET)
     */
    public function detail(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $this->success($this->service->getUserDetail(['id'=>$id]));
    }

    /**
     * 余额充值
     * @method(PUT)
     */
    public function rechargeBalance(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'mode',
            'money',
            'remark',
        ]);
        if ($this->service->rechargeToBalance($data,$id,$this->adminId,$this->adminInfo['account'])) {
            return $this->success('操作成功');
        }
        return $this->error($this->service->getError() ?: '操作失败');
    }

    /**
     * 获取用户订单
     * @noAuth(true)
     * @method(GET)
     */
    public function orderList(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $param = $this->request->getMore([]);
        $param['pay_status'] = 1;
        $this->success(OrderService::instance()->getUserOrderListByUid($id,$param));
    }
}